package com.qx.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.qx.po.Employees;
import com.qx.po.Supplier;
import com.qx.service.ISupplierService;

@Controller
public class SupplierController {
	 @Autowired
	 public ISupplierService isupplierservice;
	 
	 
	 @RequestMapping("tosupplier")
	 public String tosupplier(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Supplier> supplierlist = isupplierservice.selectSupplierAll();
		 model.addAttribute("supplierlist", supplierlist);
		 model.addAttribute("employees", employee);
		 return "/supplierinformation";
		    }
	 @RequestMapping("deletesupplier/{sno}")
	 public String deletesupplier(Model model, HttpSession session, @PathVariable("sno") String sno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 isupplierservice.deleteSupplierBysno(sno);
		 List<Supplier> supplierlist = isupplierservice.selectSupplierAll();
		 model.addAttribute("supplierlist", supplierlist);
		 model.addAttribute("employees", employee);
		 return "/supplierinformation";
	 		}
	 
	 @RequestMapping("toupdatesupplier/{sno}")
	 public String toupdatesupplier(Model model, HttpSession session, @PathVariable("sno") String sno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Supplier supplier = isupplierservice.selectSupplierBysno(sno);
		 model.addAttribute("supplier", supplier);
		 model.addAttribute("employees", employee);
		 return "/updatesupplier";
	 		}
	 
	 @RequestMapping("tosupplierinformation1/{sno}")
	 public String toupdatesupplier(Model model, @PathVariable("sno") String sno, @RequestParam("sname") String sname, @RequestParam("sns") String sns,
			 @RequestParam("saddress") String saddress, @RequestParam("stel") String stel, @RequestParam("semail") String semail,
			 @RequestParam("scontacts") String scontacts,@RequestParam("sphone") String sphone, @RequestParam("stips") String stips,
			 HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Supplier supplier = new Supplier();
		 supplier.setSno(sno);
		 supplier.setSname(sname);
		 supplier.setSns(sns);
		 supplier.setSaddress(saddress);
		 supplier.setStel(stel);
		 supplier.setSemail(semail);
		 supplier.setScontacts(scontacts);
		 supplier.setSphone(sphone);
		 supplier.setStips(stips);
		 isupplierservice.updateSupplierBysno(supplier);
		 List<Supplier> supplierlist = isupplierservice.selectSupplierAll();
		 model.addAttribute("supplierlist", supplierlist);
		 model.addAttribute("employees", employee);
		 return "/supplierinformation";
	 		}
	 @RequestMapping("toaddsupplier")
	 public String toaddsupplier(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 model.addAttribute("employees", employee);
		 return "/addsupplier";
	 		}
	 
	 @RequestMapping("addsupplier")
	 public String addsupplier(@RequestParam("sno") String sno, @RequestParam("sname") String sname, @RequestParam("sns") String sns,
			 @RequestParam("saddress") String saddress, @RequestParam("stel") String stel, @RequestParam("semail") String semail,
			 @RequestParam("scontacts") String scontacts,@RequestParam("sphone") String sphone, @RequestParam("stips") String stips,
			 Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Supplier supplier = new Supplier();
		 supplier.setSno(sno);
		 supplier.setSname(sname);
		 supplier.setSns(sns);
		 supplier.setSaddress(saddress);
		 supplier.setStel(semail);
		 supplier.setSemail(semail);
		 supplier.setScontacts(scontacts);
		 supplier.setSphone(sphone);
		 supplier.setStips(stips);
		 isupplierservice.insertSupplier(supplier);
		 List<Supplier> supplierlist = isupplierservice.selectSupplierAll();
		 model.addAttribute("supplierlist", supplierlist);
		 model.addAttribute("employees", employee);
		 return "/supplierinformation";
	 		}

}
